package com.pushdemo.logic;

import android.util.Log;

//import com.foreveross.chameleon.config.Preferences;
//import com.foreveross.chameleon.config.PushPreferences;
//import com.foreveross.chameleon.config.URL;
//import com.foss.push.socket.PushSetup;
//import com.foss.push.socket.SocketConnect;
//import com.foss.push.socket.SocketSetupCallback;
import com.pushdemo.config.ChameleonConstants;

import org.apache.cordova.CordovaActivity;
import org.apache.cordova.CordovaWebView;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
import android.app.Activity;
import android.webkit.WebView;
/**
 * Created by zhouzhineng on 14-8-22.
 * 连接变色龙服务器的业务逻辑委托类
 */
public class ChameleonLogicDelegate {

    public final static String TAG = "ChameleonLogicDelegate";
    /*private Activity activity;
    private CordovaWebView webview;
    public static ChameleonLogicDelegate mInstance;

    public static ChameleonLogicDelegate getInstance() {
        if(mInstance == null) {
            mInstance = new ChameleonLogicDelegate();
        }
        return mInstance;
    }

    private ChameleonLogicDelegate() {

    }

    public void initExecJscapacity(CordovaWebView webview) {
        this.webview = webview;
    }

    public void begin(final Activity activity) {
        this.activity = activity;
        URL.init(activity.getApplication());
        Preferences.getInstance(activity.getApplication());
        PushSetup.getInstance().setHostAndPort(ChameleonConstants.TEMP_HOST,ChameleonConstants.TEMP_PORT);
        //PushSetup.getInstance().setMsgHandler(ChameleonRegisterDelegate.msgHandler);
        //设置变色龙后台注册成功的回调
        ChameleonRegisterDelegate.setChameleonRegCallback(new ChameleonRegisterDelegate.ChameleonRegCallback() {
            @Override
            public boolean isRegSucc(boolean result,final String param) {
                //调用js开启推送插件的功能
                if(result) {
                    if(webview != null) {
                        Log.d(TAG,"ChameleonLogicDelegate->begin->isRegSucc->");

                        PushSetup.isReadyToConn = true;
                        PushSetup.getInstance().setSocketSetupCallback(new SocketSetupCallback() {
                            @Override
                            public void setupSucc(SocketConnect s) {
                                Log.d(TAG,"enter SocketSetupCallback->setupSucc");

                                ChameleonRegisterDelegate.onRegisterSucc(s,param,"");
                            }

                            @Override
                            public void setupFail(SocketConnect s) {
                                Log.d(TAG,"enter SocketSetupCallback->setupFail");
                                ChameleonRegisterDelegate.onRegisterFail();

                            }
                        });

                        //再次调用推送插件的js开启接口
                        webview.sendJavascript("var pushNotification = window.plugins.pushNotification;\n" +
                                "       var json = '{ \"xx\": \"yy\", \"zz\": jj }';\n" +
                                "       pushNotification.register(function(){}, function(){}, json);");
                    }
                    return true;
                }
                return false;
            }
        });
        authApp();
    }

    private void authApp() {
        ChameleonAuthDelegate delegate = new ChameleonAuthDelegate(activity);
        delegate.auth(new ChameleonAuthDelegate.AuthCallback() {
            @Override
            public void requestStart() {

            }

            @Override
            public void requestSuccess(String json) {
                try {
                    if(json.equals("403")) {
                        Log.e(TAG, "error code =====> 403");
                    } else if(json.equals("400")) {
                        Log.e(TAG,"error code =====> 400");
                    } else {
                        JSONObject jb = new JSONObject(json);
                        String token = jb.getString("token");
                        String expired = jb.getString("expired");

                        // 保存token和expired
                        //Preferences.saveToken(token, expired);
                        ChameleonConstants.TOKEN = token;
                        ChameleonConstants.EXPIRED = expired;
                        //PushPreferences().saveToken(token);
                        Log.e(TAG,"save token and expired");
                        Log.e(TAG,"token ==== >" + token);
                        Log.e(TAG,"expired === >" + expired);
                        // mPush.register();
                        // 检测token -> 获取token
                        ChameleonRegisterDelegate.getToken();

                    }

                } catch(JSONException e) {
                    e.printStackTrace();

                }
            }

            @Override
            public void requestFailed(String msg) {

            }
        });
    }*/
}
